package com.hn.gege.im.modules.im.service.impl;

import com.hn.gege.core.util.cache.ICacheUtils;
import com.hn.gege.im.modules.im.service.ChatWindowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ChatWindowServiceImpl implements ChatWindowService {
    private final String KEY = "window-key:";
    @Autowired
    ICacheUtils cacheUtils;


    @Override
    public LinkedList<String> listAll(String userId) {
        Map<Object, Object> hmget = cacheUtils.hmget(KEY + userId);
        List<String> collect = hmget.values().stream().map(item -> item.toString()).collect(Collectors.toList());
        return new LinkedList<String>(collect);
    }

    @Override
    public boolean add(String userId, String windowId) {
        boolean b = false;

        //判断聊天窗口是否存在
        b = cacheUtils.hHasKey(KEY + userId, windowId);
        if (b){
            return true;
        }
        b = cacheUtils.hset(KEY + userId, windowId, windowId);
        return b;
    }

    @Override
    public boolean remove(String userId, String windowId) {
        cacheUtils.hdel(KEY + userId, windowId);
        return true;
    }
}
